<?php
	ini_set('display_errors', true);
	require_once(dirname(__FILE__) . "/../controllers/user.php");
	require_once(dirname(__FILE__) . "/../controllers/news.php");
	require_once(dirname(__FILE__) . "/../model/db_driver.php");
	session_start();
	if(User::getCurrentUser()==null || User::getCurrentUser()->getRuolo()<User::$ADMIN){
		echo "Errore: devi essere un amministratore per visualizzare questa pagina.<br />";
		//var_dump(User::getCurrentUser());
	}else{
		if(isset($_REQUEST['IsPostBack'])){
		/* controlla prima di tutto se è un postback e quale azione è stata effettuata*/
			$db_driver = new DbDriver();
					
			try{
				if(isset($_REQUEST['btnAddNews'])){
					/* aggiunge la nuova news */
					$db_driver->addNews($_REQUEST['txtTitolo'], $_REQUEST['txtNews']);
				}else if(isset($_REQUEST['btnElimina'])){
					$db_driver->db_deleteNews($_POST['hdIdNews']);
				}
			}catch(Exception $ex){
				echo "<div style='color:red'>Errore: $ex</div>";
				exit();
			}
			//redireziona
		}
		?>
		<script language='javascript' type='text/javascript'>
		/*tinyMCE.init({
		        // General options
		        mode : "excat",
		        elements : "txtNews",
		        theme : "advanced",
		        plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
		
		        // Theme options
		        theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect",
		        theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		        theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
		        theme_advanced_toolbar_location : "top",
		        theme_advanced_toolbar_align : "left",
		        theme_advanced_statusbar_location : "bottom",
		        theme_advanced_resizing : true,
		
		        // Skin options
		        skin : "o2k7",
		        skin_variant : "silver",
		
		        // Example content CSS (should be your site CSS)
		        content_css : "css/example.css",
		
		        // Drop lists for link/image/media/template dialogs
		        template_external_list_url : "js/template_list.js",
		        external_link_list_url : "js/link_list.js",
		        external_image_list_url : "js/image_list.js",
		        media_external_list_url : "js/media_list.js",
		
		        // Replace values for the template plugin
		        template_replace_values : {
		                username : "Some User",
		                staffid : "991234"
		        }
		});*/
		</script>
			
				<style>
					input.buttonElimina{
						border-style:solid;
						border-width:1px;
						color:blue;
						width:60px;
					}
					
					table.newsTable{
						border-style=solid;
						border-width:1px;
						border-color:black;
					}
				</style>
				<!-- <form action='news.php' method='POST'> -->
					<input type='hidden' name='IsPostBack' value='true' />
					<p class='adminTitle'>
						News
					</p>
					<table style='width:100%'>
						<tr>
							<td style='width:100%'>
								<div>Nuova news:</div>
								<div>Titolo:&nbsp;&nbsp;<input type='text' id='txtTitolo' name='txtTitolo' value='' style='width:60%'/></div>
								<div>
									<textarea name='txtNews' id='txtNews' style='width:80%'>
									
									</textarea>
								</div>
							</td>
						</tr>
						<tr>
							<td>
								<input type='submit' name='btnAddNews' id='btnAddNews' value='Aggiungi news' onclick="aggiungiNews()"/>  <!--  -->
							</td>
						</tr>
						<tr>
							<td style='height:15px;'>&nbsp;</td>
						</tr>
					</table>
				<!-- </form> -->
				<hr style='width:100%;border-color:gray;border-style:double;border-width:2px;' />
				<br />
			<?php
						
			$news = getNews();
			$idNews =0;
			echo "<div class='panelNews'>Lista news:<br />";
			echo "<table class='newsTable' cellpadding='0' cellspacing='0'>";
			for($i=0; $i < count($news); $i++){
				$idNews = $news[$i]->id;
				echo "<tr><td class='news'>";
				echo "<span style='color:red;'>" . $news[$i]->data . "</span> - ";
				echo "<span style='color:blue'>" . $news[$i]->titolo . "</span><br />";
				echo $news[$i]->corpo;
				echo "</td>";
				echo "<td style='vertical-align:bottom;border-left: solid 1px black;border-bottom: solid 1px black;'>
						<input class='buttonElimina' type='submit' name='btnDelNews' value='Elimina' onclick='delNews($idNews);' />
					</td>
					</tr>";
			}
			echo "</table>";
			echo "</div>";
			echo "</div>";
		}					

	
?>